package com.klhappy.houyuan.utils;

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.dialog.NetWorkDialog;
import com.klhappy.houyuan.interfaces.CallBackInterModle;

import java.util.Map;

/**
 * Created by MrPu on 15/11/24.
 */
public class BaseModel{

    public Context context;

    public CallBackInterModle callBackInterModle;

    public RequestQueue mRequestQueue;

    private String cookie = null;

    private ConfigManager manager;

    private NetWorkDialog dialog;

    public BaseModel(Context context) {
        this.context = context;
        manager = new ConfigManager(context);
        cookie = KeyConstant.session;
        mRequestQueue =  Volley.newRequestQueue(context);
        dialog = new NetWorkDialog(context);
    }

    public void setCallBackInterModle(CallBackInterModle callBackInterModle) {
        this.callBackInterModle = callBackInterModle;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void Callback(final String url, final Map<String ,String> params){

        final GsonRequest<BaseJsonBean> gsonRequest = new GsonRequest<BaseJsonBean>
                (Request.Method.POST, url, BaseJsonBean.class , new Response.Listener<BaseJsonBean>() {
                    @Override
                    public void onResponse(BaseJsonBean o) {
                        callBackInterModle.CallBackFromModle(url, o);
                    }

                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        dialog.netWork("网络连接错误,请检查网络!");
                        dialog.show();
                    }
                }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //Post参数
                return params;
            }
        };

        mRequestQueue.add(gsonRequest);
    }

    public void Callback(final String url, final Map<String ,String> params ,String cookie){

        final GsonRequest<BaseJsonBean> gsonRequest = new GsonRequest<BaseJsonBean>
                (Request.Method.POST, url, BaseJsonBean.class , new Response.Listener<BaseJsonBean>() {

                    @Override
                    public void onResponse(BaseJsonBean o) {
                        callBackInterModle.CallBackFromModle(url, o);
                    }

                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        dialog.netWork("网络连接错误,请检查网络!");
                        dialog.show();
                    }
                }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //Post参数
                return params;
            }
        };

        try {
            gsonRequest.setCookie(cookie);
        } catch (AuthFailureError authFailureError) {
            authFailureError.printStackTrace();
        }
        mRequestQueue.add(gsonRequest);
    }
}
